package com.moyan.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    String sb="";
    String str ="";
    Calculate ct =new Calculate();
    Boolean drag_flag=false;
    String an ="";
    String res="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView et=findViewById(R.id.cal_edit);
        TextView result = findViewById(R.id.cal_res);
        Button b1 =findViewById(R.id.cal_num1);
        Button b2 =findViewById(R.id.cal_num2);
        Button b3 =findViewById(R.id.cal_num3);
        Button b4 =findViewById(R.id.cal_num4);
        Button b5 =findViewById(R.id.cal_num5);
        Button b6 =findViewById(R.id.cal_num6);
        Button b7 =findViewById(R.id.cal_num7);
        Button b8 =findViewById(R.id.cal_num8);
        Button b9 =findViewById(R.id.cal_num9);
        Button b0 =findViewById(R.id.cal_zero);
        Button add =findViewById(R.id.cal_add);
        Button sub =findViewById(R.id.cal_sub);
        Button mul =findViewById(R.id.cal_mul);
        Button dev =findViewById(R.id.cal_devi);
        Button del =findViewById(R.id.cal_del);
        Button ac =findViewById(R.id.cal_ac);
        Button equal =findViewById(R.id.cal_equal);
        Button exp =findViewById(R.id.cal_exp);
        Button ans =findViewById(R.id.cal_ans);
        Button point =findViewById(R.id.cal_point);
        Button left =findViewById(R.id.cal_left);
        Button right =findViewById(R.id.cal_right);
        Button root=findViewById(R.id.cal_root);
        Button power =findViewById(R.id.cal_power);
        Button square =findViewById(R.id.cal_square);
        Button sin =findViewById(R.id.cal_sin);
        Button cos =findViewById(R.id.cal_cos);
        Button tan =findViewById(R.id.cal_tan);
        Button log =findViewById(R.id.cal_log);
        Button ln =findViewById(R.id.cal_ln);
        Button arcsin =findViewById(R.id.cal_arcs);
        Button arccos =findViewById(R.id.cal_arcc);
        Button arctan =findViewById(R.id.cal_arct);
        Button fac  =findViewById(R.id.cal_fac);
        Button mode =findViewById(R.id.cal_mode);
        Button change =findViewById(R.id.cal_change);
        Button Pi =findViewById(R.id.cal_Pi);
        Button calE =findViewById(R.id.cal_e);
        Button lg =findViewById(R.id.cal_log10);
        Button fra =findViewById(R.id.cal_fra);
        et.setMovementMethod(ScrollingMovementMethod.getInstance());
        b1.setOnClickListener(v -> {
            sb+="1";
            str+="1";
            et.setText(sb);
        });
        b2.setOnClickListener(v -> {
            sb+="2";
            str+="2";
            et.setText(sb);
        });
        b3.setOnClickListener(v -> {
            sb+="3";
            str+="3";
            et.setText(sb);
        });
        b4.setOnClickListener(v -> {
            sb+="4";
            str+="4";
            et.setText(sb);
        });
        b5.setOnClickListener(v -> {
            sb+="5";
            str+="5";
            et.setText(sb);
        });
        b6.setOnClickListener(v -> {
            sb+="6";
            str+="6";
            et.setText(sb);
        });
        b7.setOnClickListener(v -> {
            sb+="7";
            str+="7";
            et.setText(sb);
        });
        b8.setOnClickListener(v -> {
            sb+="8";
            str+="8";
            et.setText(sb);
        });
        b9.setOnClickListener(v -> {
            sb+="9";
            str+="9";
            et.setText(sb);
        });
        b0.setOnClickListener(v -> {
            sb+="0";
            str+="0";
            et.setText(sb);
        });
        add.setOnClickListener(v -> {
            sb+="+";
            str+="+";
            et.setText(sb);
        });
        sub.setOnClickListener(v -> {
            sb+="-";
            str+="-";
            et.setText(sb);
        });
        mul.setOnClickListener(v -> {
            sb+="×";
            str+="×";
            et.setText(sb);
        });
        dev.setOnClickListener(v -> {
            sb+="÷";
            str+="÷";
            et.setText(sb);
        });
        del.setOnClickListener(v -> {
            if(sb.length()>=1||str.length()>=1){
                String end="";
                String end1="";
                end1=sb.substring(sb.length()-1);
                if(sb.length()>=3){
                    end =sb.substring(sb.length()-3);
                }
                if(end.equals("log")||end.equals("sin")||end.equals("cos")||end.equals("tan")||end.equals(" ln")||end.equals("mod")||end.equals(" lg")){
                    sb=sb.substring(0,sb.length()-3);
                    if(sb.length()>=1){
                        if(sb.charAt(sb.length()-1)=='a'){
                            sb=sb.substring(0,sb.length()-1);
                        }
                    }
                }else{
                    sb=sb.substring(0,sb.length()-1);
                }
                if(end1.equals("²")){
                    str=str.substring(0,str.length()-2);
                }else{
                    str =str.substring(0,str.length()-1);
                }
                et.setText(sb);
            }
        });
        ac.setOnClickListener(v->{
            sb="";
            str ="";
            et.setText(sb);
            result.setText(sb);
        });
        equal.setOnClickListener(v -> {
            res =ct.process(str,drag_flag);
            an = res;
            result.setText(res);
        });
        ans.setOnClickListener(v->{
            sb+=an;
            str+=an;
            et.setText(sb);
        });
        left.setOnClickListener(v -> {
            sb +="(";
            str+="(";
            et.setText(sb);
        });
        right.setOnClickListener(v -> {
            sb +=")";
            str+=")";
            et.setText(sb);
        });
        root.setOnClickListener(v -> {
            sb +="√";
            str+="√";
            et.setText(sb);
        });
        power.setOnClickListener(v -> {
            sb +="^";
            str+="^";
            et.setText(sb);
        });
        square.setOnClickListener(v -> {
            sb +="²";
            str+="^2";
            et.setText(sb);
        });
        sin.setOnClickListener(v -> {
            sb +="sin";
            str+="s";
            et.setText(sb);
        });
        cos.setOnClickListener(v -> {
            sb +="cos";
            str+="c";
            et.setText(sb);
        });
        tan.setOnClickListener(v -> {
            sb +="tan";
            str+="t";
            et.setText(sb);
        });
        log.setOnClickListener(v -> {
            sb +="log";
            str+="g";
            et.setText(sb);
        });
        ln.setOnClickListener(v -> {
            sb +=" ln";
            str+="l";
            et.setText(sb);
        });
        point.setOnClickListener(v ->{
            sb+=".";
            str+=".";
            et.setText(sb);
        });
        arcsin.setOnClickListener(v -> {
            sb+="asin";
            str+="a";
            et.setText(sb);
        });
        arccos.setOnClickListener(v -> {
            sb+="acos";
            str+="b";
            et.setText(sb);
        });
        arctan.setOnClickListener(v -> {
            sb+="atan";
            str+="d";
            et.setText(sb);
        });
        fac.setOnClickListener(v->{
            sb+="!";
            str+="!";
            et.setText(sb);
        });
        mode.setOnClickListener(v->{
            sb+="mod";
            str+="m";
            et.setText(sb);
        });
        change.setOnClickListener(v -> {
            if(!drag_flag){
                drag_flag=true;
                change.setText("度");
                double temp =Double.parseDouble(res);
                temp=Math.toDegrees(temp);
                res=String.valueOf(temp);
                result.setText(res);
            }else{
                drag_flag=false;
                change.setText("弧度");
                double temp =Double.parseDouble(res);
                temp= Math.toRadians(temp);
                res= String.valueOf(temp);
                result.setText(res);
            }
        });
        exp.setOnClickListener(v->{
            sb+="exp";
            str+="10^";
            et.setText(sb);
        });
        Pi.setOnClickListener(v->{
            sb+="π";
            str+="3.14159265358979323846";
            et.setText(sb);
        });
        calE.setOnClickListener(v->{
            sb+="e";
            str+="2.7182818284590452354";
            et.setText(sb);
        });
        lg.setOnClickListener(v->{
            sb+=" lg";
            str+="e";
            et.setText(sb);
        });
        fra.setOnClickListener(v->{
            double temp =Double.parseDouble(res);
            temp= 1/temp;
            res= String.valueOf(temp);
            result.setText(res);

        });
    }
}
